package com.yun.zf.system.security.handle;

import com.alibaba.fastjson.JSON;
import com.yun.zf.common.constant.HttpStatus;
import com.yun.zf.common.utils.ServletUtils;
import com.yun.zf.common.utils.StringUtils;
import com.yun.zf.common.vo.Response;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 无权访问异常处理，如没有角色不匹配则会进入当前异常处理
 */
@Component
public class AjaxAccessDeniedHandler implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException e) throws IOException, ServletException {
        String msg = StringUtils.format("请求访问111：{}，认证失败，无法访问系统资源", request.getRequestURI());
        ServletUtils.renderString(response, JSON.toJSONString(Response.error(HttpStatus.FORBIDDEN, msg)));

    }
}